package com.cyw.service;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.cyw.entity.ResourceData;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.*;

/**
 * @Author: Chenyw
 * @Date: 2022/09/07/14:30
 */
@Service
public class FileParse {

    /**
     * 解析本地文件
     *
     * @param path
     * @throws IOException
     */
    @Async
    public void parseLocalFile(String path) throws IOException {
        parseJsonReader(new FileReader(path));
    }

    private void parseJsonReader(Reader jsonReader) {
        BufferedReader reader = new BufferedReader(jsonReader);
        reader.lines().parallel().forEach(line -> {
            String webUrl = line.substring(line.indexOf("#;#") + 3).trim();
            try {
                ResourceData resourceData = new ResourceData(line.substring(0, line.indexOf("#;#")).trim(),webUrl,DownLoadResources.getLocalUrl(webUrl));
                String lineStr = JSON.toJSONString(resourceData);
                System.out.println(lineStr);
                FileUtil.appendUtf8String(lineStr+"\n","/home/chenyanwei/tvlogo.txt");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
